#include "../Thread.h"

#include <stdio.h>
#include <string>
#include <boost/bind.hpp>
//#include <boost/thread/xtime.hpp>

void threadFunc()
{
	printf("tid=%d\n", /*muduo::CurrentThread::tid()*/boost::this_thread::get_id());
}

void threadFunc2(int x)
{
	printf("tid=%d, x=%d\n", /*muduo::CurrentThread::tid()*/boost::this_thread::get_id(), x);
}

void threadFunc3()
{
	printf("tid=%d\n", /*muduo::CurrentThread::tid()*/boost::this_thread::get_id());

	boost::this_thread::sleep(boost::posix_time::seconds(1));
}

class Foo
{
public:
	explicit Foo(double x)
		: x_(x)
	{
	}

	void memberFunc()
	{
		printf("tid=%d, Foo::x_=%f\n", boost::this_thread::get_id(), x_);
	}

	void memberFunc2(const std::string& text)
	{
		printf("tid=%d, Foo::x_=%f, text=%s\n", boost::this_thread::get_id(), x_, text.c_str());
	}

private:
	double x_;
};

int thread_main() {
	printf("the main thread id: %d.\n", boost::this_thread::get_id());

	cvs::Thread t1(threadFunc);
	assert(!t1.started());
	assert(!t1.joined());

	t1.start();
	assert(t1.started());
	assert(!t1.joined());

	t1.join();
	assert(t1.started());
	assert(t1.joined());

	cvs::Thread t2(boost::bind(threadFunc2, 42), "thread for free function with argument");
	t2.start();
	t2.join();

	{
		cvs::Thread t3(threadFunc3);
		t3.start();
	}
	boost::this_thread::sleep(boost::posix_time::seconds(2));

	printf("num created thread is:%d\n", cvs::Thread::numCreated());
	assert(t1.joined());
	assert(t2.joined());
	//assert(!t3.joined());

	Foo foo(12.12);

	cvs::Thread t4(boost::bind(&Foo::memberFunc, &foo));
	t4.start();
	t4.join();

	cvs::Thread t5(boost::bind(&Foo::memberFunc2, &foo, std::string("cvs thread")));
	t5.start();
	t5.join();

	assert(cvs::Thread::numCreated()==5);

	//return 0;
	system("pause");
	return 0;
}